/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package model.xml;

import com.thoughtworks.xstream.XStream;
import common.Articulo;
import common.ArticuloStock;
import common.ENVT;
import common.OFAD;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

/**
 *
 * @author Jhonny
 */
public class ParseXML {



    public List<Articulo> getArticulosOFAD(String path) throws FileNotFoundException, IOException {
        OFAD ofad=null;
        List<Articulo> list=null;
        String datoXML = obtenerContenido(path);
        XStream xstream =  new XStream();
        xstream.autodetectAnnotations(true);
        //xstream.addDefaultImplementation(Ropa.class, Articulo.class);
        xstream.processAnnotations(OFAD.class);

        try{
        ofad = (OFAD)xstream.fromXML(datoXML);

        }catch(Exception e){
            System.out.println(""+e.getMessage());
            e.printStackTrace();

        }
        if(ofad!=null){
            list = ofad.getArticulos();
        }
        return list;
    }
    public List<ArticuloStock> getArticulosENVT(String path) throws FileNotFoundException, IOException {
        ENVT envt=null;
        List<ArticuloStock> list=null;
        XStream xstream =  new XStream();

        String datoXML = obtenerContenido(path);
        xstream.processAnnotations(ENVT.class);
        envt = (ENVT)xstream.fromXML(datoXML);
        if(envt!=null){
            list = envt.getArticulos();
        }
        return list;

    }
    private String obtenerContenido(String path) throws FileNotFoundException, IOException {
        StringBuffer contenido = new StringBuffer();
        File file = new File(path);
        if(file.exists()){
            FileInputStream fileInputStream = new FileInputStream(path);
            DataInputStream input = new DataInputStream(fileInputStream);
            while (input.available() !=0){
                contenido.append(input.readLine());
            }
            input.close();
        }
        return contenido.toString();
    }



}
